/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.notice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.notice.entity.NewsInfo;
import vip.xiaonuo.biz.modular.notice.enums.NewsStatusEnum;
import vip.xiaonuo.biz.modular.notice.enums.NoticeStatusEnum;
import vip.xiaonuo.biz.modular.notice.mapper.NewsInfoMapper;
import vip.xiaonuo.biz.modular.notice.param.*;
import vip.xiaonuo.biz.modular.notice.service.NewsInfoService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

import java.util.List;

/**
 * 新闻表Service接口实现类
 *
 * @author h
 * @date  2024/10/24 15:09
 **/
@Service
public class NewsInfoServiceImpl extends ServiceImpl<NewsInfoMapper, NewsInfo> implements NewsInfoService {
    
    @Override
    public Page<NewsInfo> page(NewsInfoPageParam newsInfoPageParam) {
        QueryWrapper<NewsInfo> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(newsInfoPageParam.getTitle())) {
            queryWrapper.lambda().like(NewsInfo::getTitle, newsInfoPageParam.getTitle());
        }
        if(ObjectUtil.isNotEmpty(newsInfoPageParam.getColumnName())) {
            queryWrapper.lambda().eq(NewsInfo::getColumnName, newsInfoPageParam.getColumnName());
        }
        if(ObjectUtil.isNotEmpty(newsInfoPageParam.getStartPublicTime()) && ObjectUtil.isNotEmpty(newsInfoPageParam.getEndPublicTime())) {
            queryWrapper.lambda().between(NewsInfo::getPubTime, newsInfoPageParam.getStartPublicTime(), newsInfoPageParam.getEndPublicTime());
        }
        if(ObjectUtil.isNotEmpty(newsInfoPageParam.getStatus())) {
            queryWrapper.lambda().eq(NewsInfo::getStatus, newsInfoPageParam.getStatus());
        }

        if(ObjectUtil.isAllNotEmpty(newsInfoPageParam.getSortField(), newsInfoPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(newsInfoPageParam.getSortOrder());
            queryWrapper.orderBy(true, newsInfoPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(newsInfoPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(NewsInfo::getNewsId);
        }

        return this.baseMapper.selectPage(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(NewsInfoAddParam newsInfoAddParam) {
        NewsInfo newsInfo = BeanUtil.toBean(newsInfoAddParam, NewsInfo.class);
        if(ObjectUtil.equal(NewsStatusEnum.PUB.getValue(),newsInfoAddParam.getStatus())){
            newsInfo.setPubTime(DateUtil.date());
        }
        this.save(newsInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(NewsInfoEditParam newsInfoEditParam) {
        NewsInfo newsInfo = this.queryEntity(newsInfoEditParam.getNewsId());
        BeanUtil.copyProperties(newsInfoEditParam, newsInfo);
        if(ObjectUtil.equal(NewsStatusEnum.PUB.getValue(),newsInfoEditParam.getStatus())){
            newsInfo.setPubTime(DateUtil.date());
        }
        this.updateById(newsInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<NewsInfoIdParam> newsInfoIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(newsInfoIdParamList, NewsInfoIdParam::getNewsId));
    }

    @Override
    public NewsInfo detail(NewsInfoIdParam newsInfoIdParam) {
        return this.queryEntity(newsInfoIdParam.getNewsId());
    }

    @Override
    public NewsInfo queryEntity(String id) {
        NewsInfo newsInfo = this.getById(id);
        if(ObjectUtil.isEmpty(newsInfo)) {
            throw new CommonException("新闻表不存在，id值为：{}", id);
        }
        return newsInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updStatus(NewsInfoStatusParam newsInfoStatusParam) {
        NewsInfo newsInfo = this.queryEntity(newsInfoStatusParam.getId());
        newsInfo.setStatus(newsInfoStatusParam.getStatus());
        if(ObjectUtil.equal(NewsStatusEnum.PUB.getValue(),newsInfoStatusParam.getStatus())){
            newsInfo.setPubTime(DateUtil.date());
        }
        this.updateById(newsInfo);
    }
}
